//
//  ViewController.m
//  UniversalAddressBook
//
//  Created by zhenjingxiong on 2019/1/14.
//  Copyright © 2019 zhenjingxiong. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSArray *firstArray = [self readLocalFileWithName:@"guishudi"];
    NSLog(@"%@",firstArray);
    
    NSMutableArray *loactionArray = [NSMutableArray array];
    
    static int i = 0;
    
    for (NSString *phone in firstArray) {
        
        //  http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel=13308622037
        NSMutableDictionary *info =[NSMutableDictionary new];
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSString *url = [NSString stringWithFormat:@"http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel=%@",phone];
        [manager GET:url parameters:info progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSLog(@"%@",responseObject);
            NSMutableDictionary *info=[NSMutableDictionary new];
            
            NSString *phoneKey = [[responseObject[@"response"] allKeys] firstObject];
            NSString *locationStr;
            if (responseObject[@"response"][phoneKey] == [NSNull null]) {
                locationStr = @"没有查询到";
            }else{
                locationStr = responseObject[@"response"][phoneKey][@"location"];
            }
            
            //            NSString *locationStr = [self DataTOjsonString:responseObject[key][@"location"]];
            
            if (locationStr && phoneKey) {
                [info setValue:locationStr forKey:phoneKey];
            }else{
                [info setValue:@"没有查询到" forKey:phoneKey];
            }
            
            
            [loactionArray addObject:info];
            
            i++;
            
            
            if (i >= firstArray.count) {
                NSLog(@"++++++++++++++++%@",loactionArray);
                NSMutableArray *phoneLoacationArray = [NSMutableArray array];
                NSMutableArray *phoneArray = [NSMutableArray array];
                NSMutableArray *ValueArray = [NSMutableArray array];
                for (NSDictionary *info in loactionArray) {
                    [info enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                        [phoneLoacationArray addObject:[NSString stringWithFormat:@"手机号码:%@  地理位置:%@",key,obj]];
                        [phoneArray addObject:key];
                        [ValueArray addObject:obj];
                    }];
                }
                NSLog(@"******************%@",phoneArray);
                NSLog(@"******************%@",ValueArray);
            }
            
            
            //            NSLog(@"%@",[self descriptionWithLocale:loactionArray]);
            
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            
        }];
        
        
        
    }
    
}

-(NSString *)descriptionWithLocale:(NSArray *)locale
{
    NSMutableString * stringM=[NSMutableString string];
    [stringM appendString:@"(\r\n"];
    [locale enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(idx!=locale.count-1)
        {
            [stringM appendFormat:@"\t%@,\r\n",obj];
        }
        else
        {
            [stringM appendFormat:@"\t%@\r\n",obj];
        }
    }];
    [stringM appendString:@")"];
    return stringM.copy;
    
}



// 读取本地JSON文件
- (NSArray *)readLocalFileWithName:(NSString *)name {
    // 获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"json"];
    // 将文件数据化
    NSData *data = [[NSData alloc] initWithContentsOfFile:path];
    // 对数据进行JSON格式化并返回字典形式
    return [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
}



@end
